home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / DevTools / NibEditor / extract.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-12  |  2.1 KB  |  129 lines

  1. /*
  2.  * extract.c:
  3.  *
  4.  * Make a list of all of the nibs in an executable; extract them and put
  5.  * them into files ending with ".nib"
  6.  *
  7.  */
  8.  
  9. #include <libc.h>
  10.  
  11. FILE    *nibfile;
  12. char    **niblist;
  13. int    nibs=0;
  14. char    *prog;
  15.  
  16.  
  17. void    usage()
  18. {
  19.     printf("usage: extract <program-to-extract>\n");
  20.     exit(1);
  21. }
  22.  
  23. void    extract(char *nibname)
  24. {
  25.     char    buf[512];
  26.     char    filename[512];
  27.     char    *cc;
  28.     struct    stat    sbuf;
  29.  
  30.     strcpy(filename,nibname);
  31.  
  32.     /* see if the last four characters are ".nib".  If not, then add
  33.      * them...
  34.      */
  35.  
  36.     printf("Extracting %s ");
  37.  
  38.     cc    = filename+strlen(filename);
  39.     if(cc[-4]!='.'
  40.        || cc[-3]!='n'
  41.        || cc[-2]!='i'
  42.        || cc[-1]!='b'){
  43.         strcat(filename,".nib");
  44.         printf("--> %s ",filename);
  45.     }
  46.  
  47.     sprintf(buf,"/bin/segedit -extract __NIB %s %s %s",
  48.         nibname,filename,prog);
  49.     system(buf);
  50.  
  51.     stat(filename,&sbuf);
  52.     printf("\n");
  53.     fprintf(nibfile,"%s %s %ld\n",nibname,filename,sbuf.st_mtime);
  54. }
  55.  
  56. /* Process a section; capture the section name and look for
  57.  * segment name __NIB
  58.  */
  59. void    do_section(FILE *pout)
  60. {
  61.     char    sectname[256];
  62.     char    segname[256];
  63.     char    *cc;
  64.  
  65.     sectname[0]    = '\000';
  66.     segname[0]    = '\000';
  67.     
  68.     fgets(sectname,sizeof(sectname),pout);
  69.     if(strncmp(sectname,"  sectname ",11)){
  70.         printf("section: %s",sectname);
  71.         return;
  72.     }
  73.  
  74.     if((cc=index(sectname,'\n'))!=0){
  75.         *cc = '\000';
  76.     }
  77.  
  78.     fgets(segname,sizeof(segname),pout);
  79.     if(strcmp(segname,"   segname __NIB\n"))    return;
  80.  
  81.     /* I've got a NIB; add it to the list. */
  82.  
  83.     niblist        = realloc(niblist,sizeof(char *) * (nibs+1));
  84.     niblist[nibs]    = malloc(strlen(sectname)+1);
  85.     strcpy(niblist[nibs],sectname+11);
  86.     nibs++;
  87.     return;
  88. }
  89.  
  90. int    main(int argc,char **argv)
  91. {
  92.     FILE    *pout;
  93.     char    *buf;
  94.     int    i;
  95.  
  96.     if(argc!=2){
  97.         usage();
  98.     }
  99.  
  100.     nibfile    = fopen("NIBLIST","w");
  101.  
  102.     niblist    = malloc(0);
  103.     nibs    = 0;
  104.  
  105.     prog     = argv[1];
  106.     buf    = alloca(strlen(prog)+20);
  107.     sprintf(buf,"otool -l %s",prog);
  108.  
  109.     pout    = popen(buf,"r");
  110.     while(!feof(pout)){
  111.         char    lbuf[256];
  112.  
  113.         fgets(lbuf,sizeof(lbuf),pout);
  114.         if(!strcmp(lbuf,"Section\n")){
  115.             do_section(pout);
  116.         }
  117.     }
  118.     pclose(pout);
  119.  
  120.     printf("Program: %s\nnibs: %d\n",prog,nibs);
  121.  
  122.     /* Now extract each nib... */
  123.     for(i=0;i<nibs;i++){
  124.         extract(niblist[i]);
  125.     }
  126.  
  127. }
  128.  
  129.